function todosNull(valores) {
    var resultado = true;
    var i = 0;

    for (i = 0; i < valores.length; i++) {
        if (valores[i] != null && valores[i] != '') {
            resultado = false;
            break;
        }
    }
    return resultado;
    return false;
}

function isNull(valor) {
    return (valor == null || valor == '');
}

function checkAllCheckBoxesInTable(inputId, state) {	
    var commonIdPart = inputId.substr(0, inputId.lastIndexOf(':'));
    var tableId = commonIdPart;
    var tableElement = document.getElementById(tableId);
    var inputs = tableElement.getElementsByTagName('input');
    for ( var i = 0; i <= inputs.length; i++) {
        var input = inputs[i];
        if (input != undefined) {
            if (input.getAttribute('type') == 'checkbox' && state) {
                input.setAttribute('checked', state);
            } else {
                input.setAttribute('checked', false);
                input.removeAttribute('checked');
            }
        }
    }
}

function teste(event, campo){
    return mask(true, event, campo, '#####-###', 9);
}

function formataCEP(event, campo) {
    return mask(true, event, campo, '#####-###');
}   

function FormataCPFCNPJ(campo,teclapres) {
    if (campo.value.length < 15 ) {
        if (campo.value.length < 14) {
            FormataCPF(campo,teclapres);
        } else if (campo.value.length == 14) {
            num = String(campo.value.substr(3,1));
            //alert("num: "+num)
            if (num != ".") {
                //alert("cnpj");
                FormataCNPJ(campo,teclapres);
            } else {
                //alert("cnpf");
                FormataCPF(campo,event);
            }
        }
    }
    else {
        FormataCNPJ(campo,teclapres);
    }
}
   
function formataData(Campo, teclapres) {
    var tecla = teclapres.keyCode;
    var vr = new String(Campo.value);

    if ("0123456789".search(vr.substr(vr.length - 1, 1)) == -1) {
        vr = vr.substr(0, vr.length - 1);
        Campo.value = vr;
    } else {
        vr = vr.replace("/", "");
        vr = vr.replace("/", "");
        vr = vr.replace("/", "");
        tam = vr.length + 1;
        if (tecla != 9 && tecla != 8) {
            if (tam > 2 && tam < 5) {
                Campo.value = vr.substr(0, tam - 2) + '/'
                + vr.substr(tam - 2, tam);
            }
            if (tam >= 5 && tam <= 10) {
                Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/'
                + vr.substr(4, 4);
            }
        }
    }
}


function FormataCPF(campo,teclapres) {
    var tecla = teclapres.keyCode;
    vr = campo.value;
    if ("0123456789".search(vr.substr(vr.length-1,1)) == -1) {
        vr = vr.substr(0, vr.length-1);
        campo.value = vr;
    } else {
        vr = vr.replace( ".", "" );
        vr = vr.replace( ".", "" );
        vr = vr.replace( "-", "" );
        tam = vr.length+1;
        if ( tecla != 9 && tecla != 8 ) {
            if ( tam > 3 && tam < 7 ) {
                campo.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3 );
            }
            if ( tam >= 7 && tam < 10 ) {
                campo.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3, 3 ) + '.' + vr.substr( 6 );
            }
            if ( tam >= 10 && tam <= 11 ) {
                campo.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3, 3 ) + '.' + vr.substr( 6, 3 ) + '-' + vr.substr( 9 );
            }
        }
        // usado no onChange para completar se o usuario tiver colado o numero
        if (campo.value.length == 11) {
            var cpf = campo.value;
            cpf = cpf.substr( 0, 3 ) + '.' + cpf.substr( 3, 3 ) + '.' + cpf.substr( 6, 3 ) + '-' + cpf.substr( 9 );
            campo.value = cpf;
        }
    }
}   

function FormataCNPJ(campo,teclapres) {
    var tecla = teclapres.keyCode;
    vr = campo.value;
    if ("0123456789".search(vr.substr(vr.length-1,1)) == -1) {
        vr = vr.substr(0, vr.length-1);
        document.form[campo].value = vr;
    }
    else {
        vr = vr.replace( ".", "" );
        vr = vr.replace( ".", "" );
        vr = vr.replace( "/", "" );
        vr = vr.replace( "-", "" );
        tam = vr.length+1;
        if ( tecla != 9 && tecla != 8 ) {
            if ( tam > 1 && tam < 4 ) {
                campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2 );
            }
            if ( tam > 4 && tam < 7 ) {
                campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5 );
            }
            if ( tam > 7 && tam < 11 ) {
                campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8 );
            }
            if ( tam > 11 && tam <= 14 ) {
                campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, 4 ) + "-" + vr.substr( 12 );
            }
        }
        // usado no onChange para completar se o usuario tiver colado o numero
        if (campo.value.length == 14) {
            var cnpj = campo.value;
            cnpj = cnpj.substr( 0, 2 ) + '.' + cnpj.substr( 2, 3 ) + '.' + cnpj.substr( 5, 3 ) + '/' + cnpj.substr( 8, 4 ) + "-" + cnpj.substr( 12 );
            campo.value = cnpj;
        }
    }
}

function autoTab(campo, keyCode, length) {
    var i = campo.value.length;
    if (i == length - 1) {
        campo.value += String.fromCharCode(keyCode);
        var form = campo.form;
        for (var i = 0; i < form.elements.length; i++) {
            if (form.elements[i] == campo) {
                campo.blur();
                if ((form.elements[i + 1] != null) && (form.elements[i + 1].name != "METHOD"))
                    form.elements[i + 1].focus();
                break;
            }
        }
        return false;
    } else {
        return true;
    }
}

function mask(somenteNumeros, event, campo, mask, maxLength) {
    var keyCode;
	 
    if (event.srcElement)
        keyCode = event.keyCode;
    else if (event.target)
        keyCode = event.which;
	
    var maskStack = new Array();
	
    var isDynMask = false;
	
    if (mask.indexOf('[') != -1)
        isDynMask = true;
	
    var length = mask.length;
	
    for (var i = 0; i < length; i++)
        maskStack.push(mask.charAt(i));
	
    var value = campo.value;
    var i = value.length;
	 
    if (keyCode == 0 || keyCode == 8)
        return true;
	
    if (somenteNumeros && (keyCode < 48 || keyCode > 57))
        return false;
	
    if (maxLength != undefined && value.length > maxLength) {
        return false;
    }
	
    if (!isDynMask && i < length) {
	
        if (maskStack.toString().indexOf(String.fromCharCode(keyCode)) != -1 && keyCode != 8) {
            return false;
        } else {
            if (keyCode != 8) {
                if (maskStack[i] != '#') {
                    var old = campo.value;
                    campo.value = old + maskStack[i];
                }
            }
		
            if (autoTab(campo, keyCode, length)) {
                if (!document.layers) {
                    return true;
                } else if (keyCode != 8) {
                    campo.value += String.fromCharCode(keyCode);
                    return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        }
	
    } else if (isDynMask) {
	
        var maskChars = "";
        for (var j = 0; j < maskStack.length; j++)
            if (maskStack[j] != '#' && maskStack[j] != '[' && maskStack[j] != ']')
                maskChars += maskStack[j];
	
        var tempValue = "";
        for (var j = 0; j < value.length; j++) {
            if (maskChars.indexOf(value.charAt(j)) == -1)
                tempValue += value.charAt(j);
        }
	
        value = tempValue + String.fromCharCode(keyCode);
	
        if (maskChars.indexOf(String.fromCharCode(keyCode)) != -1) {
            return false;
        } else {
	
            var staticMask = mask.substring(mask.indexOf(']') + 1);
            var dynMask = mask.substring(mask.indexOf('[') + 1, mask.indexOf(']'));
	
            var realMask = new Array;
	
            if (mask.indexOf('[') == 0) {
	 
                var countStaticMask = staticMask.length - 1;
                var countDynMask = dynMask.length - 1;

                for (var j = value.length - 1; j >= 0; j--) {
                    if (countStaticMask >= 0) {
                        realMask.push(staticMask.charAt(countStaticMask));
                        countStaticMask--;
                    }
                    if (countStaticMask < 0) {
                        if (countDynMask >= 0) {
                            if (dynMask.charAt(countDynMask) != '#') {
                                realMask.push(dynMask.charAt(countDynMask));
                                countDynMask--;
                            }
                        }
				 
                        if (countDynMask == -1) {
                            countDynMask = dynMask.length - 1;
                        }
				
                        realMask.push(dynMask.charAt(countDynMask));
                        countDynMask--;
                    }
                }
            }
	
            var result = "";
	
            var countValue = 0;
            while (realMask.length > 0) {
                var c = realMask.pop();
                if (c == '#') {
                    result += value.charAt(countValue);
                    countValue++;
                } else {
                    result += c;
                }
            }
	
            campo.value = result;

            if (maxLength != undefined && value.length == maxLength) {
                var form = campo.form;
                for (var i = 0; i < form.elements.length; i++) {
                    if (form.elements[i] == campo) {
                        campo.blur();
                        if ((form.elements[i + 1] != null) && (form.elements[i + 1].name != "METHOD"))
                            form.elements[i + 1].focus();
                        break;
                    }
                }
            }
            return false;
        }
    } else {
        return false;
    }
}
/*
 * FUNCOES PARA TRATAR OS IFRAMES QUE DAO SUPORTE A CRIACAO DE PDF
 **/

/*
 * CADA PAGINA DE CRIACAO DE PDF DEVE DAR SUPORTE A FUNCAO DE VALIDACAO E INVOCACAO DAS DEMAIS FUNCOES.
 * ABAIXO UM EXEMPLO PADRAO. OBSERVE TODOS OS PARAMETROS PASSADOS PARA AS DEMAIS FUNCOES. 
 * EX:
 * function iframePDF ( ) {
    var parametros = "";
    if($F("form:numeroDocumento").length >  0){
        parametros += "&numeroDocumentoParameter=" + $F("form:numeroDocumento");
    }
    (parametros != "") ? displayIframePDF("form","relatorio-processo-pdf.seam?",parametros, "600px", "2px solid silver") : hidenIframePDF("form");
}
 **/

function renderizaIframe(idIframe,height, border, width){ 
    window.parent.document.getElementById(idIframe).style.height = (height != null ? height : "0px");
    window.parent.document.getElementById(idIframe).style.border = (border != null ? border : "0px");
    window.parent.document.getElementById(idIframe).style.width = (width != null ? width : "100%");
}

function hidenIframePDF (idIframe){
    document.getElementById(idIframe).src = "";
    renderizaIframe(idIframe);
}

function displayIframePDF (idIframe, url, parametros, height, border, width){
    document.getElementById(idIframe).src = url + parametros;
    renderizaIframe(idIframe,height, border, width);
}

/* Adicao da funcao trim e seus derivados na classe String */

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

/*Funcao que permite apenas numeros*/
function numero(string){
    return string.replace(/\D/g,"");
}

function mascara(obj, mascara) {
    var campo = obj.value.length;
    var saida = mascara.substring(0,1);
    var texto = mascara.substring(campo);
    if(texto.substring(0,1) != saida) {
        obj.value += texto.substring(0,1);
    }
}

function numProcessoMask(obj){
    mascara(obj, "###.######/####");
}

function dataMenorQueASegunda(dataString1, dataString2){
    try{
        var data1 = dataString1.split("/");
        var data2 = dataString2.split("/");
        
        var isMaior = false;
        
        //Comparacao do ano
        if (data2[2] > data1[2]) {
        	isMaior = true;
        } else if (data2[2] < data1[2]) {
        	isMaior = false;
        } else if (data2[2] == data1[2]) {
        	// Comparacao do mes
        	if (data2[1] > data1[1]) {
            	isMaior = true;
            } else if (data2[1] < data1[1]) {
            	isMaior = false;
            } else if (data2[1] == data1[1]) {
            // Comparacao do dia
            	if (data2[0] > data1[0]) {
                	isMaior = true;
                } else if (data2[0] < data1[0]) {
                	isMaior = false;
                } else if (data2[0] = data1[0]){
                	isMaior = true;
                }
            }
        }
        return isMaior;
    }catch(ex){
    	return false;
    }
}